package com.gopay.bis.service.bank.impl.wap;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.bis.manager.bank.Wap.CibWapB2cManager;
import com.gopay.common.Result;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.cib.ebank.CibMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.WapPayInteractRemoteService;

/** 
 * 兴业银行wap支付service
 * @ClassName: CibWapPayB2CBankServiceImpl 
 * @author w_jiqing 
 * @date 2017年3月16日 下午3:09:49  
 */ 
public class CibWapPayB2CBankServiceImpl implements WapPayInteractRemoteService {
    
    @Resource(name = "cibWapB2cManager")
    private CibWapB2cManager cibWapB2cManager;

    @Override
    public Result pay(BaseMessage base) {
        Result result = new Result(false);
        try {
            CibMessage messageB2C = new CibMessage();
            BeanUtils.copyProperties(base, messageB2C);
            messageB2C = (CibMessage) cibWapB2cManager.sign(messageB2C);
            result.setDefaultModel(messageB2C);
            result.setSuccess(true);
        } catch (GopayException e) {
            BankLogUtils.printServiceImplExceptionLog(e);
            result.setResultCode(e.getErrCode());
        } catch (Exception ex) {
            BankLogUtils.printServiceImplExceptionLog(ex);
            result.setResultCode(ProcCodeConstants.PROC_CODE_100E6029);
        }
        return result;
    }

    @Override
    public Result bankBack(BaseMessage base) {
        Result result = new Result(false);
        try {
            CibMessage messageB2C = new CibMessage();
            BeanUtils.copyProperties(base, messageB2C);
            boolean flag = (Boolean) cibWapB2cManager.verify(messageB2C);
            result.setModel("message", messageB2C);
            result.setDefaultModel(flag);
            result.setSuccess(true);
            if (flag) {
                Map<String, String> param = new HashMap<String, String>();
                // 收到银行通知，代表支付成功
                param.put("orderId", messageB2C.getOrderId());
                param.put("amt", messageB2C.getPayAmt());
                param.put("cardType", "Unknown");
                param.put("notifyNum", "1");
                param.put("payStatus", "00");
                param.put("retCode", "00");
                param.put("retMsg", "验签成功");
                param.put("signData", messageB2C.getSignMessage());
                param.put("isFront", String.valueOf(base.isFront()));
                result.setModel("isNotifyCashier", param);
            }
        } catch (GopayException e) {
            BankLogUtils.printServiceImplExceptionLog(e);
            result.setResultCode(e.getErrCode());
        } catch (Exception ex) {
            BankLogUtils.printServiceImplExceptionLog(ex);
            result.setResultCode(ProcCodeConstants.PROC_CODE_100E6064);
        }
        return result;
    }

    @Override
    public Result queryBack(BaseMessage base) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Result query(BaseMessage base) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Result refund(BaseMessage base) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Result dayend(BaseMessage base) {
        // TODO Auto-generated method stub
        return null;
    }

}
